package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.IAlertDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 报警数据Service业务层处理
 * 
 * @author mr.wu
 * @date 2025-05-29
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }


    /**
     * 处理报警数据
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<AlertData> handleAlertData(AlertDataDto dto) {
        // 报警数据校验
        if (Objects.isNull(dto.getId()) || Objects.isNull(dto.getProcessingResult()) || Objects.isNull(dto.getProcessingTime())) {
            throw new IllegalArgumentException("关键数据不能为空");
        }
        List<AlertData> alertDataList = this.lambdaQuery()
                .eq(AlertData::getId, dto.getId()) //获取报警数据id
                .list();
        Long userId = SecurityUtils.getUserId();//获取用户id
        String username = SecurityUtils.getUsername();//获取用户名称
        for (AlertData alertData : alertDataList) {
            alertData.setProcessingResult(dto.getProcessingResult());//设置处理结果
            alertData.setId(dto.getId());//设置报警数据id
            alertData.setProcessingTime(dto.getProcessingTime());//设置处理时间
            alertData.setStatus(1);//设置处理状态
            alertData.setProcessorId(userId);//设置用户id
            alertData.setProcessorName(username);//设置用户名称
        }
        this.updateBatchById(alertDataList);
        return alertDataList;
    }


}
